Project : Customer Lifetime Value Analysis¶

In [1]:
import pandas as pd
import matplotlib as plt
import seaborn as sns
import matplotlib.pyplot as plt
import plotly.graph_objs as go
import plotly.express as px
import plotly.io as pio

pio.templates.default ="plotly_white"
In [2]:
data = pd.read_csv("customer_acquisition_data.csv")
data.head()
Out[2]:
customer_id channel cost conversion_rate revenue
0 1 referral 8.320327 0.123145 4199
1 2 paid advertising 30.450327 0.016341 3410
2 3 email marketing 5.246263 0.043822 3164
3 4 social media 9.546326 0.167592 1520
4 5 referral 8.320327 0.123145 2419

Visualising the distribution for acquistion cost and revenue generated by the customer using histograms:¶

In [3]:
# histogram for cost and revenue generated by the customer
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(12, 4))
sns.histplot(data['cost'], bins=5, kde=True, color='skyblue', ax=axes[0])
axes[0].set_xlabel('Cost')
axes[0].set_ylabel('Count')
axes[0].set_title('Acquisition Cost Distribution')

sns.histplot(data['revenue'], bins=5, kde=True, color='lightgreen', ax=axes[1])
axes[1].set_xlabel('Revenue Generated')
axes[1].set_ylabel('Count')
axes[1].set_title('Revenue Generated Distribution')
Out[3]:
Text(0.5, 1.0, 'Revenue Generated Distribution')
In [4]:
# horizontal bar chart showing cost of acquisition across various channels

cost_by_channel = data.groupby('channel')['cost'].sum().reset_index()
colors = ['skyblue', 'lightgreen', 'lightcoral', 'lightsalmon']

plt.barh(cost_by_channel["channel"],cost_by_channel["cost"],color=colors)
plt.xlabel('Acquisition Cost'),
plt.ylabel('Channels'),
plt.title('Acquisition Cost by Channel')
plt.show()
In [5]:
#Horizontal bar graph determining which channels are most and least effective at converting customers
conversion_by_channel = data.groupby('channel')['conversion_rate'].sum().reset_index()
colors = ['skyblue', 'lightgreen', 'lightcoral', 'lightsalmon']

plt.barh(conversion_by_channel["channel"],conversion_by_channel["conversion_rate"],color=colors)
plt.xlabel('Conversion rate'),
plt.ylabel('Channels'),
plt.title('Conversion rate by Channel')
plt.show()

Calculating the total revenue by channel and finding the most and least profitable channels in terms of generating revenue¶

In [6]:
#Pie chart visualisation determining total revenue by channel
revenue_by_channel = data.groupby('channel')['revenue'].sum().reset_index()

fig = px.pie(revenue_by_channel,
             values='revenue',
             names='channel' ,
             title='Total Revenue by Channel',
             hole =0.4, color_discrete_sequence =px.colors.qualitative.Pastel)
fig.show()
In [7]:
## Calculating the return on investment (ROI) for each Channel
data['ROI'] = data['revenue']/data['cost']
ROI_by_channel = data.groupby('channel')['ROI'].sum().reset_index()
colors = ['skyblue', 'lightgreen', 'lightcoral', 'lightsalmon']

plt.barh(ROI_by_channel ["channel"],ROI_by_channel["ROI"],color=colors)
plt.xlabel('Return on Investment'),
plt.ylabel('Channels'),
plt.title('ROI by Channel')
plt.show()
In [ ]: